package cn.com.sise.controller;

import cn.com.sise.dao.ClassDao;
import cn.com.sise.pojo.Class;
import cn.com.sise.pojo.Major;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class ClassController
{
@Resource
private ClassDao classDao;

//查询所有专业信息
@RequestMapping("/findAllClasses")//映射地址
public String findAllClasses(Model model)
{
List<Class> classes=classDao.findAllClasses();
System.out.println("获取到数据量："+classes.size());
model.addAttribute("cls",classes);
return "/admin/showAllClasses.jsp";
};//function findAllClasses

//跳转到添加页面
//@RequestMapping(value = "/addClassP")
@GetMapping("/addClassP")//当用get形式提交请求的时候
public String toAddClass(){return "/admin/addClass.jsp";};//function toAddClass

//添加班级信息
//@RequestMapping(value = "/addClassM",method = RequestMethod.POST)
@PostMapping("/addClassM")
public String addClassM(Class c)
{
int rows=classDao.addClass(c);
return "redirect:/findAllClasses";//跳转到另一个方法（直接返回页面则不用加redirect）
};//function addClassM

//查询班级信息
@RequestMapping(value = "/findClassById/{classId}")
public String findClassById
(@PathVariable("classId") int classId , Model model)
{
Class c=classDao.findClassByClassId(classId);
model.addAttribute("cls",c);
//System.out.println("获取到"+c);
return "/admin/updateClass.jsp";
};//function findClassById

//修改班级信息
@RequestMapping(value = "/updateClassM")
public String updateClassM(Class cls)
{
System.out.println("updateClassM:\n"+cls);
int rows=classDao.updateClass(cls);
System.out.println("影响行数："+rows);
return "redirect:/findAllClasses";
};//function updateClassM

//根据majorId删除专业信息
@RequestMapping(value = "/delClassById/{classId}")
public String delClassById(@PathVariable("classId") int classId)//注意形参匹配
{
//System.out.println("要删除的id："+classId);
int rows=classDao.deleteClassById(classId);
//System.out.println("删除数据："+rows);
return "redirect:/findAllClasses";
};//function deleteMajorByMajorId

//批量删除
@RequestMapping("/delClasses")
public String deleteClassByIds(String[] id)
{
//    System.out.println("获取到的id：");
//    for(String i:id){System.out.print(i+" ");}
//    System.out.println();
int r= classDao.deleteClassesByIds(id);
System.out.println("影响行数："+r);
return "redirect:/findAllClasses";
};//function deleteClassByIds


};//class ClassController
